# encoding: utf-8
#@author: sydr
#@file: addDemo.py
#@time: 2024/8/5 10:45
import json
import random
from time import sleep
import mitmproxy
from mitmproxy.http import Request, Response
from mitmproxy import ctx

from addon.CPA_evaluation import CPAEvaluation
from utils.sign import singWeb


class Demo:
    def request(self, flow: mitmproxy.http.HTTPFlow):
        r:Request = flow.request

        if 'jxuserapi/user/info' in r.url:

            ctx.log.info("打印内容打印内容打印内容打印内容："+str(r.query.get("versionCode")))
            r.headers.update({"sydr_reuqest":"测试改请求头"})
            r.query.update({"version":"改query参数"})

        if "userapi/planner/getPlannerTopicDetailVo/front" in r.url:
            data = json.loads(r.get_text())
            data["version"]="修改请求体参数"
            r.text = json.dumps(data)


    def response(self, flow: mitmproxy.http.HTTPFlow):
        res: Response = flow.response
        r: Request = flow.request
        if 'jxuserapi/user/info' in r.pretty_url:
            # 如果解码报错修改 编码方式
            data = json.loads(res.get_text().encode('gbk'))
            data["data"]["name"] = "测试修改响应"
            res.text = json.dumps(data)
            res.headers.update({"sydr_response":"测试改响应头"})

class ChangeEnv:
    def __init__(self,env):
        self.env=env


    def request(self, flow: mitmproxy.http.HTTPFlow):
        r:Request = flow.request
        if "//api.juexiaotime.com" in r.url:
            r.url=r.url.replace("//api.juexiaotime.com","//%sapi.juexiaotime.com"%(self.env))
        if "//api.zhanliujiang.com" in r.url:
            r.url=r.url.replace("//api.zhanliujiang.com","//%sapi.zhanliujiang.com"%(self.env))
        if "//centerapi.juexiaotime.com" in r.url:
            r.url = r.url.replace("//centerapi.juexiaotime.com", "//center%sapi.juexiaotime.com"%(self.env))
        if "//userapi.juexiaotime.com" in r.url:
            r.url = r.url.replace("//userapi.juexiaotime.com", "//%suserapi.juexiaotime.com"%(self.env))
        if "//crmapi.juexiaotime.com" in r.url:
            r.url = r.url.replace("//crmapi.juexiaotime.com", "//crm%sapi.juexiaotime.com"%(self.env))


# ifmodify=True

class TestHttp:
    def request(self, flow: mitmproxy.http.HTTPFlow):
        r:Request = flow.request


    def response(self, flow: mitmproxy.http.HTTPFlow):
        res: Response = flow.response
        r: Request = flow.request


class versionModify:
    def request(self, flow: mitmproxy.http.HTTPFlow):
        r: Request = flow.request
        if "zhanliujiang" in r.pretty_url:
            # cpa需要手动把请求头的版本号改为和后台一样
            r.headers.update({"versionCode":"182"})

    def response(self, flow: mitmproxy.http.HTTPFlow):
        res: Response = flow.response
        r: Request = flow.request
        # cpa屏蔽版本更新
        if "/commonapi/cmsVersion/front/latestVersion" in r.pretty_url:
            data = json.loads(res.get_text())
            data["data"]["upgradeVersionCode"]=str(int(r.headers.get("versionCode"))+1)
            data["data"]["isForceUpgrade"]=0
            res.text = json.dumps(data)
            
        if "userapi/StudyVersion/getLatestVersion" in r.pretty_url:
            data = json.loads(res.get_text())
            data["data"]["versionCode"]=r.headers.get("versionCode")
            data["data"]["updateHint"]=False
            data["data"]["isForce"]=2
            res.text = json.dumps(data)


class openSign:
    def request(self, flow: mitmproxy.http.HTTPFlow):
        r: Request = flow.request
        if r.headers.get("s") and int(r.headers.get("s"))>0:
            if r.headers.get("u") and int(r.headers.get("u"))>0:

                headersMap=singWeb(r.headers.get("s"),r.headers.get("u"))
            else:
                headersMap = singWeb(r.headers.get("s"))
            r.headers.pop("s")
            r.headers.pop("t")
            r.headers.pop("signature")
            r.headers.pop("n")
            headersMap["Source"]="web"
            r.headers.update(headersMap)


addons = [
# Demo(),
#ChangeEnv("rel"),
TestHttp(),
# openSign()
versionModify()
]


